package com.ruoyi.gpt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.core.domain.User;
import com.ruoyi.core.domain.vo.UserVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.gpt.domain.bo.GptUserBo;
import com.ruoyi.gpt.domain.vo.GptUserVo;
import com.ruoyi.gpt.domain.GptUser;
import com.ruoyi.gpt.mapper.GptUserMapper;
import com.ruoyi.gpt.service.IGptUserService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * chatGpt用户Service业务层处理
 *
 * @author ztx
 * @date 2023-03-17
 */
@RequiredArgsConstructor
@Service
public class GptUserServiceImpl implements IGptUserService {

    private final GptUserMapper baseMapper;

    /**
     * 查询chatGpt用户
     */
    @Override
    public GptUserVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询chatGpt用户列表
     */
    @Override
    public TableDataInfo<GptUserVo> queryPageList(GptUserBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<GptUser> lqw = buildQueryWrapper(bo);
        Page<GptUserVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询chatGpt用户列表
     */
    @Override
    public List<GptUserVo> queryList(GptUserBo bo) {
        LambdaQueryWrapper<GptUser> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<GptUser> buildQueryWrapper(GptUserBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<GptUser> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getNickname()), GptUser::getNickname, bo.getNickname());
        lqw.eq(StringUtils.isNotBlank(bo.getIsFollow()), GptUser::getIsFollow, bo.getIsFollow());
        return lqw;
    }

    /**
     * 新增chatGpt用户
     */
    @Override
    public Boolean insertByBo(GptUserBo bo) {
        GptUser add = BeanUtil.toBean(bo, GptUser.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改chatGpt用户
     */
    @Override
    public Boolean updateByBo(GptUserBo bo) {
        GptUser update = BeanUtil.toBean(bo, GptUser.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(GptUser entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除chatGpt用户
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public GptUserVo getByOpenIdAndAppid(String openId,String appid) {
        LambdaQueryWrapper<GptUser> qw = Wrappers.lambdaQuery();
        qw.eq(GptUser::getOpenid,openId);
        qw.eq(GptUser::getAppid,appid);
        return baseMapper.selectVoOne(qw);
    }

    @Override
    public GptUserVo setGptUser(String appid,String unionid, String openId, String uType, String nickname, String avatar) {
        GptUser gptUser = new GptUser();
        gptUser.setAppid(appid);
        gptUser.setUnionid(unionid);
        gptUser.setOpenid(openId);
        gptUser.setUtype(uType);
        gptUser.setNickname(nickname);
        gptUser.setAvatar(avatar);
        gptUser.setLastLoginTime(new Date());
        gptUser.setGptTotal(0);
        gptUser.setGptRemaining(5);
        gptUser.setIdentity("0");
        baseMapper.insert(gptUser);

        return BeanUtil.toBean(gptUser, GptUserVo.class);
    }
}
